package tests;

import reflect.pojo.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @author:雷淑娥
 * @Date:2024/12/5 11:53
 * version:1.0
 */
public class Test01 {
    public static void main(String[] args) {
        try {
            Class clz = Person.class; //获取Class实例

            System.out.println("类名：" + clz.getName());
            System.out.println("修饰符：" + Modifier.toString(clz.getModifiers()));

            //构造方法信息
            Constructor[] cons = clz.getDeclaredConstructors();
            System.out.println("===构造方法展示===");
            for(Constructor con : cons) {
                System.out.print("访问修饰符：");
                int modifier = con.getModifiers();
                //判断构造方法的访问修饰符
                if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
                    System.out.println("public");
                else if ((modifier & Modifier.PROTECTED) == Modifier.PROTECTED)
                    System.out.println("protected");
                else if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                    System.out.println("private");
                else
                    System.out.println("default(package)");
            }

            //属性信息
            Field[] fields = clz.getDeclaredFields();
            System.out.println("===属性===");
            for (Field field : fields) {
                System.out.println("属性名：" + field.getName());
                System.out.println("类型：" + field.getType().getName());
                System.out.print("访问修饰符：");
                int modifier = field.getModifiers();
                //判断构造方法的访问修饰符
                if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
                    System.out.println("public");
                else if ((modifier & Modifier.PROTECTED) == Modifier.PROTECTED)
                    System.out.println("protected");
                else if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                    System.out.println("private");
                else
                    System.out.println("default(package)");
            }

            //方法信息
            Method[] methods = clz.getDeclaredMethods();
            System.out.println("===方法===");
            for (Method method : methods) {
                System.out.println("方法名：" + method.getName());
                System.out.println("返回值类型：" + method.getReturnType().getName());
                System.out.print("访问修饰符：");
                int modifier = method.getModifiers();
                //判断构造方法的访问修饰符
                if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
                    System.out.println("public");
                else if ((modifier & Modifier.PROTECTED) == Modifier.PROTECTED)
                    System.out.println("protected");
                else if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                    System.out.println("private");
                else
                    System.out.println("default(package)");
            }
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
